import sys
from PyQt6.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton


class MainWindow(QWidget):#继承QWidget
    def __init__(self, *args, **kwargs):#self is default parameter;you can add other parameters;
        #*args is mean any parameters; *kwargs is mean any dictronary parameters;
        super().__init__()#继承执行父类的构造函数
        self.resize(400, 300)#设置窗口大小
        self.setWindowTitle("button控件") #set window title;be overrides the title
        layout = QVBoxLayout() #创建一个垂直布局
        self.button1 = QPushButton("第一个按钮")#创建一个按钮
        self.button1.setCheckable( True)#设置按钮为选中状态
        #self.button1.setEnabled( False)#设置按钮为禁用状态
        self.button1.setText("第一个按钮!")#重新设置按钮文本
        #self.button1.clicked.connect(button1_clicked)#按钮点击
        self.button1.pressed.connect(self.button1_pressed)#按钮按下
        self.button1.released.connect(self.button1_released)#按钮释放
        self.button1.toggled.connect(self.button1_toggled)#按钮状态切换 这个不对,没反应 #setCheckable( True)需要这样
        layout.addWidget(self.button1)#添加按钮到布局
        self.setLayout(layout)#添加布局到窗口

        bg_color = kwargs.get('setbackgroundColor', None)
        if bg_color:
            self.setStyleSheet(f"background-color: {bg_color};")
        self.show()
    def button1_clicked(self):
        print("button1 clicked")
    def button1_pressed(self):
        print("button1 pressed")
    def button1_released(self):
        print("button1 released")
    def button1_toggled(self):
        if self.button1.isChecked():
            #print("button1 checked")
            self.button1.setStyleSheet("background-color: red;")
        else:
            self.button1.setStyleSheet("background-color: lightgray;")
        #print("button1 toggled")

if __name__ == "__main__":
    #print(sys.argv) #['D:\\u\\test\\gitee\\pythonqt\\test1\\改造程序\\new_hello2.py'] print sys.argv;default is this file path.
    app=QApplication(sys.argv)#get parameters from command line.
    w=MainWindow(setbackgroundColor="lightblue") #use MainWindow created a window.
    w.setWindowTitle("button控件")
    w.show()#显示窗口
    #app.exec()#运行程序

    sys.exit(app.exec()) #app.exec() is first stop,then out process

    #import time #test app end,but process is not stop.
    #for i in range(10):#genarate a dictionary
    #    print(i)
    #    time.sleep(1)
# app=QApplication([])
# window=QWidget(windowTitle="Hello World",)#创建一个窗口
# window.show()#显示窗口
# app.exec()#运行程序
